<template>
  <van-cell class="comment-item" :title="commentInfo.content">
    <van-image
      class="avatar"
      slot="icon"
      round
      :src="commentInfo.aut_photo"
      fit="cover"
    />
    <div slot="title">
      <div class="name-line">
        <div class="name">{{ commentInfo.aut_name }}</div>
        <div class="like-wrap" @click="onCommentLike">
          <van-icon
            :color="commentInfo.is_liking ? 'red' : ''"
            :name="commentInfo.is_liking ? 'good-job' : 'good-job-o'"
          />
          <span>{{ commentInfo.like_count }}</span>
        </div>
      </div>

      <div class="content">
        {{ commentInfo.content }}
      </div>
      <div>
        <span class="pubdate">{{ commentInfo.pubdate | formatMonthDay }}</span>
        <van-button
          class="reply-btn"
          round
          size="mini"
          @click="$emit('reply-click', commentInfo)"
          >{{ commentInfo.reply_count }} 回复</van-button
        >
      </div>
    </div>
  </van-cell>
</template>

<script>
import { addCommentLike, removeCommentLike } from '@/api/comment'
export default {
  name: 'CommentItem',
  props: {
    commentInfo: {
      type: Object,
      required: true
    }
  },
  data() {
    return {}
  },
  methods: {
    async onCommentLike() {
      // 已点赞，取消点赞，未点赞，点赞
      if (this.commentInfo.is_liking) {
        const { data } = await removeCommentLike(this.commentInfo.com_id)
        console.log(data)
        this.commentInfo.is_liking = false
        this.commentInfo.like_count--
      } else {
        const { data } = await addCommentLike(this.commentInfo.com_id)
        console.log(data)
        this.commentInfo.is_liking = true
        this.commentInfo.like_count++
      }
    }
  }
}
</script>

<style lang="less" scoped>
.comment-item {
  .avatar {
    width: 36px;
    height: 36px;
    margin-right: 13px;
  }
  .name-line {
    display: flex;
    justify-content: space-between;
    .name {
      font-size: 14px;
      color: #406599;
    }
    .like-wrap {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .content {
    font-size: 16px;
    color: #222222;
  }
  .pubdate {
    font-size: 10px;
  }
  .reply-btn {
    margin-left: 10px;
  }
}
</style>
